import { MagicWeaponBase } from "./MagicWeaponBase";
import { MagicWeaponItemData } from "./MagicWeaponItemData";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { ResolveItemRenderer } from "./renderer/ResolveItemRenderer";
import { ItemData } from "../../resource/info/ItemData";
import { RefreshVo } from "./data/RefreshVo";
import { Alert } from "../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { PulseSharedObject } from "../player/PulseSharedObject";
import { Cache } from "../../cache/Cache";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { CheckBoxWin } from "../common/alertwins/CheckBoxWin";
import { Language } from "../../../../extend/language/Language";
import { MsgManager } from "../../manager/MsgManager";
import { RefreshAmuletReplaceVo } from "./data/RefreshAmuletReplaceVo";
import { EPlayerItemPosType } from "../../../../Message/Public/EPlayerItemPosType";
import { MagicWeaponUtil } from "./MagicWeaponUtil";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { DisplayUtil } from "../../../common/DisplayUtil";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { RefreshPreViewVo } from "./RefreshPreViewVo";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { ObjCreate } from "../common/ObjCreate";
import { WordBgItem } from "./WordBgItem";
import { BaseItem } from "../common/BaseItem";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { ScrollPolicy } from "../../../../flashx/textLayout/container/ScrollPolicy";
import { MWPropertyItemRenderer } from "./renderer/MWPropertyItemRenderer";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { WuXingQualityBuyPanel } from "../wuxing/WuXingQualityBuyPanel";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { QuickBuyWindow } from "../common/QuickBuyWindow";
import { GButton } from "../../../../com/mui/controls/GButton";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
import { GTileList } from "../../../../com/mui/controls/GTileList";
import { GCheckBox } from "../../../../com/mui/controls/GCheckBox";
type int = number;
//class MagicWeaponRefreshPanel
    
    export  class MagicWeaponRefreshPanel extends MagicWeaponBase
    {
        constructor()
        {
            super();
            this.init();
            return;
        }

        /* internal  */onPutDownRefreshItem(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this._addNum = 0;
            this._addNumHasResponse = false;
            this.addNumTxtShow(0, false);
            this._refreshItem.data = new MagicWeaponItemData(null);
            this._refreshMaterialItemShow.itemData = null;
            this._tipTxt.mouseEnabled = true;
            this._curItemList.dataProvider = new DataProvider([]);
            this._curItemList.drawNow();
            this._refreshMaterialItem.setItemCode(0);
            this._filterMaterialData = _materialData.filter(this.refreshItemFilterFun);
            this.drawList = this._filterMaterialData;
            if (arg1) 
            {
                this.setSelect();
            }
            return;
        }

        /* internal  */getUId(arg1): string
        {
            var loc1=null;
            if (arg1 && arg1 is ResolveItemRenderer && arg1.data) 
            {
                loc1 = arg1.data as MagicWeaponItemData;
                if (loc1 && loc1.itemData) 
                {
                    return loc1.itemData.uid;
                }
            }
            else if (arg1 && arg1 is ItemData) 
            {
                return arg1.uid;
            }
            return "";
        }

        /* internal  */setSelect(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (loc1 && loc1.itemData && (loc1.itemData.uid == this.getUId(this._refreshItem) || loc1.itemData.uid == this.getUId(this._refreshMaterialItem.itemData))) 
                {
                    loc1.isSelect = true;
                    continue;
                }
                loc1.isSelect = false;
            }
            this.drawList = this._filterMaterialData;
            return;
        }

        /* internal  */set drawList(arg1: Array<any>)
        {
            _materialList.dataProvider = new DataProvider(arg1);
            _materialList.drawNow();
            if (pageSelecter) 
            {
                pageSelecter.maxPage = int(Math.ceil(arg1.length / 6));
                currentPage();
            }
            return;
        }

        /* internal  */onRefreshHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var itemData: ItemData;
            var onBtnClickHandler: Function;
            var vo: RefreshVo;
            var isNeedAlert: Boolean;

            var loc1;
            onBtnClickHandler = null;
            vo = null;
            isNeedAlert = false;
            e = arg1;
            onBtnClickHandler = (arg1: int, arg2: Boolean): void
            {
                if (arg1 != Alert.OK) 
                {
                    if (arg1 == Alert.CANCEL) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponRefresh, vo));
                    }
                }
                else 
                {
                    onReplaceHandler();
                }
                if (arg2) 
                {
                    PulseSharedObject.save("MagicWeaponRefresh", true);
                }
                return;
            }
            if (!this.isCanOperate) 
            {
                return;
            }
            itemData = this._refreshItem.itemData;
            if (itemData && this._refreshMaterialItem.itemData) 
            {
                if (this._material.itemData.amount < 1 && !this._autoBuyMateria.selected) 
                {
                    this.showQuickBuyWindow();
                    return;
                }
                vo = new RefreshVo();
                vo.autoBuy = this._autoBuyMateria.selected ? 1 : 0;
                vo.srcPosType = itemData.posType;
                vo.desPosType = this._refreshMaterialItem.itemData.posType;
                vo.mainAmuletUid = itemData.uid;
                vo.assistantAmuletUid = this._refreshMaterialItem.itemData.uid;
                vo.materials = Cache.instance.pack.backPackCache.getUidListByType(ECategory._ECategoryProp, EProp._EPropAmuletRefreshStone);
                if (this._addNum > 0) 
                {
                    isNeedAlert = !PulseSharedObject.isTodayNotTips("MagicWeaponRefresh");
                    if (isNeedAlert) 
                    {
                        Alert.alertWinRenderer = CheckBoxWin;
                        Alert.okLabel = Language.getString(27707);
                        Alert.calcelLabel = Language.getString(27637);
                        Alert.show(Language.getString(27636), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                        return;
                    }
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponRefresh, vo));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(27714));
            }
            return;
        }

        /* internal  */onReplaceHandler(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            var e: Object /* flash.events.MouseEvent */=null;
            var itemData: ItemData;
            var onBtnClickHandler: Function;
            var vo: RefreshAmuletReplaceVo;
            var isNeedAlert: Boolean;

            var loc1;
            onBtnClickHandler = null;
            vo = null;
            isNeedAlert = false;
            e = arg1;
            onBtnClickHandler = (arg1: int, arg2: Boolean): void
            {
                if (arg1 == Alert.OK) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponRefreshReplace, vo));
                }
                if (arg2) 
                {
                    PulseSharedObject.save("MagicWeaponReplace", true);
                }
                return;
            }
            if (!this.isCanOperate) 
            {
                return;
            }
            itemData = this._refreshItem.itemData;
            if (itemData) 
            {
                if (this._xiLianItemList.dataProvider.length <= 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(27634));
                    return;
                }
                vo = new RefreshAmuletReplaceVo();
                vo.srcPosType = itemData.posType;
                vo.mainAmuletUid = itemData.uid;
                isNeedAlert = !PulseSharedObject.isTodayNotTips("MagicWeaponReplace");
                if (isNeedAlert) 
                {
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.show(Language.getString(27635), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponRefreshReplace, vo));
            }
            return;
        }

        /* internal  */refreshItemFilterFun(arg1, arg2: int, arg3: Array<any>): Boolean
        {
            var loc1;
            return (loc1 = arg1 as MagicWeaponItemData).itemData.posType == EPlayerItemPosType._EPlayerItemPosTypeRole || loc1.itemData.magicWeaponLv > 0 && loc1.itemData.magicWeaponScore > 0;
        }

        /* internal  */refreshMaterialItemFilterFun(arg1, arg2: int, arg3: Array<any>): Boolean
        {
            var loc1=MagicWeaponItemData(arg1).itemData;
            var loc2;
            if ((loc2 = this._refreshItem.itemData) == null) 
            {
                return false;
            }
            return !(loc1.posType == EPlayerItemPosType._EPlayerItemPosTypeRole) && loc1.magicWeaponLv <= 0 && loc2.type == loc1.type && loc2.color == loc1.color && loc2.magicWeaponScore >= loc1.magicWeaponScore;
        }

        public /* override */ updateWeaponInfo(): void
        {
            var loc1=null;
            var loc4=null;
            var loc2=Cache.instance.magicWeapon.hasIdentifyAndWear;
            var loc3=Cache.instance.magicWeapon.hasIdentifyAndNoWear;
            loc2.sort(MagicWeaponUtil.sortData);
            loc3.sort(MagicWeaponUtil.sortData);
            loc1 = loc2.concat(loc3);
            var loc5=0;
            var loc6=loc1;
            for(loc4 of loc6) 
            {
                if (loc4.itemData.uid == this.getUId(this._refreshItem)) 
                {
                    this._refreshItem.data = loc4;
                    loc4.isSelect = true;
                }
                if (loc4.itemData.uid != this.getUId(this._refreshMaterialItem.itemData)) 
                {
                    continue;
                }
                loc4.isSelect = true;
            }
            if (this._refreshItem.itemData) 
            {
                this._filterMaterialData = loc1.filter(this.refreshMaterialItemFilterFun);
            }
            else 
            {
                this._filterMaterialData = loc1.filter(this.refreshItemFilterFun);
            }
            if (_materialList && loc1 && this._filterMaterialData) 
            {
                _materialData = loc1;
                this.drawList = this._filterMaterialData;
            }
            if (this._buyPanel) 
            {
                this._buyPanel.updatePackData();
            }
            this.updatePackData();
            return;
        }

        public refreshAndReplaceSucceed(arg1: ItemData, arg2: Boolean=false): void
        {
            if (arg1 == null) 
            {
                return;
            }
            this._addNum = 0;
            this._addNumHasResponse = false;
            this.addNumTxtShow(0, false);
            this._refreshMaterialItem.setItemCode(0);
            this._refreshMaterialItemShow.visible = true;
            this._tipTxt.mouseEnabled = true;
            this._refreshItem.data = new MagicWeaponItemData(arg1);
            this.listData = arg1;
            return;
        }

        public refreshPreViewSucceed(arg1: int): void
        {
            this._addNumHasResponse = true;
            this._addNum = arg1;
            if (this._xiLianItemList.dataProvider.length > 0) 
            {
                this.addNumTxtShow(arg1, true);
            }
            else 
            {
                this.addNumTxtShow(0, false);
            }
            return;
        }

        /* internal  */addNumTxtShow(arg1: int, arg2: Boolean): void
        {
            if (arg2) 
            {
                if (arg1 > 0) 
                {
                    this._addNumTxt.htmlText = Language.getStringByParam(27638, arg1);
                    this._arrow.bitmapData = GlobalClass.getBitmapData(ImagesConst.RangeRaise);
                }
                else 
                {
                    this._addNumTxt.htmlText = Language.getStringByParam(27639, arg1);
                    this._arrow.bitmapData = GlobalClass.getBitmapData(ImagesConst.RangeDecline);
                }
                this.addChild(this._addNumTxt);
                this.addChild(this._arrow);
            }
            else 
            {
                DisplayUtil.removeMe(this._addNumTxt);
                DisplayUtil.removeMe(this._arrow);
            }
            return;
        }

        /* internal  */get isCanOperate(): Boolean
        {
            return this._xiLianItemList.dataProvider == null || this._xiLianItemList.dataProvider.length <= 0 || this._xiLianItemList.dataProvider.length > 0 && this._addNumHasResponse;
        }

        /* internal  */set listData(arg1: ItemData)
        {
            this._curItemList.dataProvider = new DataProvider(MagicWeaponUtil.nextLvProperty(arg1, arg1.color, arg1.magicWeaponLv));
            this._xiLianItemList.dataProvider = new DataProvider(MagicWeaponUtil.nextLvProperty(arg1, arg1.color, arg1.magicWeaponLv, false, false, true));
            return;
        }

        protected /* override */ onEquipmentPageChange(arg1: Object /* flash.events.Event */): void
        {
            var loc1=_materiaPageSelecter.currentPage;
            var loc2=(loc1 - 1) * 6;
            var loc3=this._filterMaterialData.length - loc2 > 6 ? 6 : this._filterMaterialData.length - loc2;
            var loc4=[];
            var loc5=loc2;
            while (loc5 < loc2 + loc3) 
            {
                loc4[loc5 - loc2] = this._filterMaterialData[loc5];
                ++loc5;
            }
            _materialList.dataProvider = new DataProvider(loc4);
            _materialList.drawNow();
            return;
        }

        protected /* override */ onSelectItemHandler(arg1: ListEvent): void
        {
            var loc2=null;
            var loc1=arg1.item as MagicWeaponItemData;
            if (loc1) 
            {
                if (MagicWeaponUtil.magicWeaponCanXiLian(loc1.itemData)) 
                {
                    this._refreshItem.data = loc1;
                    this._refreshMaterialItemShow.itemData = loc1.itemData;
                    this._refreshMaterialItemShow.visible = true;
                    this._refreshMaterialItemShow.strengthLabelVisible = false;
                    this._tipTxt.mouseEnabled = true;
                    this.listData = loc1.itemData;
                    this._refreshMaterialItem.setItemCode(0);
                    this._filterMaterialData = _materialData.filter(this.refreshMaterialItemFilterFun);
                    this.drawList = this._filterMaterialData;
                    this._addNumHasResponse = false;
                    loc2 = new RefreshPreViewVo();
                    loc2.posType = loc1.itemData.posType;
                    loc2.amuletUid = loc1.itemData.uid;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponRefreshPreView, loc2));
                }
                else 
                {
                    if (this._refreshItem.itemData == null) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27713));
                        return;
                    }
                    if (this._refreshItem.itemData.type != loc1.itemData.type) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27711));
                        return;
                    }
                    if (this._refreshItem.itemData.color != loc1.itemData.color) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27711));
                        return;
                    }
                    if (this._refreshItem.itemData.magicWeaponScore < loc1.itemData.magicWeaponScore) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27711));
                        return;
                    }
                    this._refreshMaterialItem.itemData = loc1.itemData;
                    this._refreshMaterialItemShow.visible = false;
                    this._tipTxt.mouseEnabled = false;
                }
                this.setSelect();
            }
            return;
        }

        /* internal  */init(): void
        {
            var loc1=new GImageBitmap(ImagesConst.MagicWeaponBg, 43, 188);
            this.addChild(loc1);
            var loc2=new GImageBitmap("equipment_recast_recast.png", 193, 205);
            this.addChild(loc2);
            this._refreshItem = new ResolveItemRenderer(false);
            this._refreshItem.addEventListener(flash.events.MouseEvent.CLICK, this.onPutDownRefreshItem);
            ObjCreate.setObjAttri(this._refreshItem, 65, 175, -1, -1, this);
            this._refreshMaterialItem = new WordBgItem();
            this._refreshMaterialItem.word = Language.getString(27708);
            this._refreshMaterialItem.addEventListener(flash.events.MouseEvent.CLICK, this.onPutDownRefreshMaterialItem);
            ObjCreate.setObjAttri(this._refreshMaterialItem, 290, 207, -1, -1, this);
            this._refreshMaterialItemShow = new BaseItem();
            this._refreshMaterialItemShow.mouseEnabled = false;
            this._refreshMaterialItemShow.visible = false;
            this._refreshMaterialItemShow.filters = [FilterConst.colorFilter2];
            ObjCreate.setObjAttri(this._refreshMaterialItemShow, 293, 210, 32, 32, this);
            this._tipTxt = ObjCreate.createGTextField("   ", 293, 210, 32, 32, this);
            this._tipTxt.toolTipData = Language.getString(27715);
            this._material = new WordBgItem(14160200);
            this._material.word = Language.getString(27709);
            ObjCreate.setObjAttri(this._material, 345, 207, -1, -1, this);
            ObjCreate.createBg(30, 255, 140, 145, this);
            ObjCreate.createBg(267, 255, 140, 145, this);
            this._curItemList = ObjCreate.createTileList(60, 255, 80, 200, this);
            this._curItemList.scrollPolicy = ScrollPolicy.OFF;
            this._curItemList.columnWidth = 100;
            this._curItemList.rowHeight = 22;
            this._curItemList.setStyle("cellRenderer", MWPropertyItemRenderer);
            this._xiLianItemList = ObjCreate.createTileList(300, 255, 130, 200, this);
            this._xiLianItemList.scrollPolicy = ScrollPolicy.OFF;
            this._xiLianItemList.columnWidth = 130;
            this._xiLianItemList.rowHeight = 22;
            this._xiLianItemList.setStyle("cellRenderer", MWPropertyItemRenderer);
            var loc3=GlobalClass.getBitmap(ImagesConst.EquipRecast_arrow);
            ObjCreate.setObjAttri(loc3, 196, 312, -1, -1, this);
            this._refreshBtn = ObjCreate.createGButton(Language.getString(27706), 130, 406, 72, 26, this, "ButtonRed");
            this._refreshBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onRefreshHandler);
            this._replaceBtn = ObjCreate.createGButton(Language.getString(27707), 232, 406, 72, 26, this, "ButtonRed");
            this._replaceBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onReplaceHandler);
            ObjCreate.createGTextField(Language.getString(27666), 304, 411, 80, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createTextBox("20000", 338, 410, 70, 20, this, GlobalStyle.textFormatWhite);
            ObjCreate.createBitmap(ImagesConst.Tongqian, 389, 411, this);
            ObjCreate.createBg(31, 434, 380, 2, this, ImagesConst.SplitLine);
            ObjCreate.createFireTip(Language.getString(27710), 218, 458, this, true);
            ObjCreate.createBg(26, 437, 185, 46, this);
            this._buyPanel = new WuXingQualityBuyPanel(ECategory._ECategoryProp, EProp._EPropAmuletRefreshStone, false, false, false);
            this._buyPanel.updateNeedCount(1);
            ObjCreate.setObjAttri(this._buyPanel, 29, 420, -1, -1, this);
            this._autoBuyMateria = ObjCreate.createCheckBox(Language.getString(27664), 220, 435, 100, 28, this, "GRadioButton");
            var loc4;
            (loc4 = ObjCreate.createImgBtn(ImagesConst.RuleExplain, 350, 440, 60, 23, this, "Button")).gbutton.toolTipData = Language.getString(27712);
            loc4.gbutton.addEventListener(flash.events.MouseEvent.CLICK, this.clickHelpHandler);
            this._addNumTxt = ObjCreate.createGTextField("", 270, 382, 180, 20, this);
            this._arrow = ObjCreate.createGBitmap(ImagesConst.RangeRaise, 393, 382, this);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.clean);
            LoaderHelp.addResCallBack(ResFileConst.magicWeapon, this.onLoadResHandler);
            return;
        }

        /* internal  */clickHelpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponShowPageGuide));
            return;
        }

        /* internal  */onLoadResHandler(): void
        {
            var loc1=GlobalClass.getBitmap(ImagesConst.MagicWeaponRefresh);
            ObjCreate.setObjAttri(loc1, 59, 180, -1, -1, this);
            var loc2=GlobalClass.getBitmap(ImagesConst.MagicWeaponRefreshMaterial);
            ObjCreate.setObjAttri(loc2, 296, 180, -1, -1, this);
            return;
        }

        /* internal  */clean(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (!(loc1 && loc1.isSelect)) 
                {
                    continue;
                }
                loc1.isSelect = false;
            }
            operateMaterialData = [];
            this.onPutDownRefreshItem();
            this.onPutDownRefreshMaterialItem();
            if (arg1 && pageSelecter) 
            {
                restPage();
            }
            return;
        }

        /* internal  */showQuickBuyWindow(): void
        {
            QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropAmuletRefreshStone, -1, -1);
            QuickBuyWindow.instance.updateDefaultAmount(1);
            QuickBuyWindow.instance.show();
            return;
        }

        /* internal  */updatePackData(): void
        {
            var loc1=Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropAmuletRefreshStone);
            this._material.itemData.amount = loc1;
            this._material.filters = loc1 != 0 ? null : [FilterConst.colorFilter];
            return;
        }

        /* internal  */onPutDownRefreshMaterialItem(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this._refreshMaterialItem.setItemCode(0);
            this._refreshMaterialItemShow.visible = true;
            this._tipTxt.mouseEnabled = true;
            if (arg1) 
            {
                this.setSelect();
            }
            return;
        }

        /* internal */ /* var */_filterMaterialData: Array<any>;

        /* internal */ /* var */_refreshBtn: GButton;

        /* internal */ /* var */_replaceBtn: GButton;

        /* internal */ /* var */_refreshItem: ResolveItemRenderer;

        /* internal */ /* var */_refreshMaterialItem: WordBgItem;

        /* internal */ /* var */_material: WordBgItem;

        /* internal */ /* var */_refreshMaterialItemShow: BaseItem;

        /* internal */ /* var */_tipTxt: GTextFiled;

        /* internal */ /* var */_curItemList: GTileList;

        /* internal */ /* var */_xiLianItemList: GTileList;

        /* internal */ /* var */_autoBuyMateria: GCheckBox;

        /* internal */ /* var */_buyPanel: WuXingQualityBuyPanel;

        /* internal */ /* var */_addNum: int;

        /* internal */ /* var */_addNumTxt: GTextFiled;

        /* internal */ /* var */_arrow: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_addNumHasResponse: Boolean;
    }
